package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.bean.SetSecurityBeanTing;
import com.example.dilidili_ting.bean.loginResultBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.thecode.aestheticdialogs.AestheticDialog;
import com.thecode.aestheticdialogs.DialogStyle;
import com.thecode.aestheticdialogs.DialogType;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChangePasswordTing extends AppCompatActivity {

    private EditText editOldPswTing;
    private EditText editNewPswTing;
    private EditText editNewPswAgainTing;
    private Button btnZhmmTing;
    private ImageView ivBack2Ting;
    private TextView titleBarNameTing;
    private ImageView ivXiaokeaiTing;
    private String ting_userName; //用户名变量
    private String OldPswTing,NewPswTing,NewPswAgainTing; //旧密码，新密码，新密码确认字符串变量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password_ting);
        //获取当前登录用户的用户名称
        ting_userName= SharedPreferencesUtilsTing.readLoginUserName(this);
        initView();
    }

    private void initView() {
        ivBack2Ting = (ImageView) findViewById(R.id.iv_back2_ting);
        titleBarNameTing = (TextView) findViewById(R.id.title_bar_name_ting);
        ivXiaokeaiTing = (ImageView) findViewById(R.id.iv_xiaokeai_ting);
        editOldPswTing = (EditText) findViewById(R.id.edit_old_psw_ting);
        editNewPswTing = (EditText) findViewById(R.id.edit_new_psw_ting);
        editNewPswAgainTing = (EditText) findViewById(R.id.edit_new_psw_again_ting);
        btnZhmmTing = (Button) findViewById(R.id.btn_zhmm_ting);
        titleBarNameTing.setText("修改密码");
        ivXiaokeaiTing.setBackgroundResource(R.drawable.boy);
        ivBack2Ting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChangePasswordTing.this.finish();
            }
        });
        //按钮点击事件
        btnZhmmTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
            }
        });
    }

    /**
     * 获取用户输入字符串
     * */
    private void getEditString(){
        OldPswTing=editOldPswTing.getText().toString().trim();
        NewPswTing=editNewPswTing.getText().toString().trim();
        NewPswAgainTing=editNewPswAgainTing.getText().toString().trim();
    }
    /**
     * 用户修改密码功能
     * */
    private void click(){
        getEditString();
        if (TextUtils.isEmpty(OldPswTing)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("ERROR")
                    .setMessage("请输入旧密码")
                    .setDuration(1500)
                    .show();
            return;
        }else if (TextUtils.isEmpty(NewPswTing)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("ERROR")
                    .setMessage("请输入新密码")
                    .setDuration(1500)
                    .show();
            return;
        }else if (TextUtils.isEmpty(NewPswAgainTing)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("ERROR")
                    .setMessage("请输入再一次新密码")
                    .setDuration(1500)
                    .show();
            return;
        }else if (!NewPswTing.equals(NewPswAgainTing)){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("ERROR")
                    .setMessage("两次输入的密码不一致")
                    .setDuration(1500)
                    .show();
            return;
        }else if (!OldPswTing.equals(SharedPreferencesUtilsTing.readPsw(ChangePasswordTing.this,ting_userName))){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("ERROR")
                    .setMessage("输入的密码与原始密码不一样")
                    .setDuration(1500)
                    .show();
            return;
        }else if (NewPswTing.equals(SharedPreferencesUtilsTing.readPsw(ChangePasswordTing.this,ting_userName))){
            new AestheticDialog.Builder(this, DialogStyle.TOASTER, DialogType.WARNING)
                    .setTitle("ERROR")
                    .setMessage("输入的新密码与旧密码不能一样")
                    .setDuration(1500)
                    .show();
            return;
        }else {
            JsonObject jo = new JsonObject();
            //登录的用户名
            String userName_ting = SharedPreferencesUtilsTing.readLoginUserName(this);
            jo.addProperty("userName",userName_ting);
            jo.addProperty("oldpwd",OldPswTing);
            jo.addProperty("newpwd",NewPswTing);
//            jo.addProperty("kfz","zxt");
            OkhttpUtilsTing.isPOST("AndroidUser/updatePwd",false, jo, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {}

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res = response.body().string();
                    Gson gson = new Gson();
                    final loginResultBeanTing<String> r = gson.fromJson(res, loginResultBeanTing.class);
                    ChangePasswordTing.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if(r.code == 200){
                                new AestheticDialog.Builder(ChangePasswordTing.this, DialogStyle.EMOTION, DialogType.SUCCESS)
                                        .setTitle("SUCCESS")
                                        .setMessage("修改密码成功")
                                        .setDuration(2000)
                                        .show();
                                SharedPreferencesUtilsTing.editPsw(ChangePasswordTing.this,NewPswTing,ting_userName);//修改用户保存在SharedPreferences中的密码
                                //设置跳转时长
                                Timer timer=new Timer();
                                TimerTask task=new TimerTask() {
                                    @Override
                                    public void run() {
                                        Intent intent=new Intent(ChangePasswordTing.this, loginActivityTing.class);//定义用户登录页面意图
                                        startActivity(intent); //打开登录页面
//                              tingSettingActivity.instance.finish(); //关闭设置页面
                                        ChangePasswordTing.this.finish();//关闭修改密码页面
                                    }
                                };
                                timer.schedule(task,2000);
                            }else {
                                new AestheticDialog.Builder(ChangePasswordTing.this, DialogStyle.EMOTION, DialogType.ERROR)
                                        .setTitle("ERROR")
                                        .setMessage(r.msg)
                                        .setDuration(2000)
                                        .show();
                            }
                        }
                    });
                }
            });
        }
    }

}